#!/bin/bash

# Start server with gunicorn

#!/bin/bash
set -e

# === Configuration ===
APP_MODULE="app:app"                     # Replace with your app’s WSGI module if different
BIND="0.0.0.0:8080"
WORKERS=2                                # Adjust based on cpu cores
LOG_DIR="./logs"                         # Change to absolute path in production
ERROR_LOG="$LOG_DIR/gunicorn-error.log"
ACCESS_LOG="$LOG_DIR/gunicorn-access.log"


# === Auto-update codebase ===
echo "Updating codebase with git pull..."
git pull || { echo "git pull failed, continuing with existing code."; }

# === Setup ===
mkdir -p "$LOG_DIR"
echo "Starting Gunicorn with:
  Bind:       $BIND
  Workers:    $WORKERS
  Error log:  $ERROR_LOG
  Access log: $ACCESS_LOG
"

# === Run Gunicorn ===
exec gunicorn \
  --bind "$BIND" \
  --workers "$WORKERS" \
  --error-logfile "$ERROR_LOG" \
  --access-logfile "$ACCESS_LOG" \
  --capture-output \
  --log-level info \
  "$APP_MODULE"

